#include "MonsterRoom.hh"
#include <iostream>
#include <cstdlib>

MonsterRoom::MonsterRoom(std::string name, std::string description)
    : Room(name, description) {}

void MonsterRoom::addEnemy(const Enemy& enemy) {
    enemies.push_back(enemy);
}

bool MonsterRoom::hasEnemies() const {
    return !enemies.empty();
}

void MonsterRoom::enterRoom(Player& player) {
    std::cout << "\nYou enter the monster room: " << getName() << "\n";
    descriptionText();

    // 随机选择1到2个怪物
    // Choose 1 or 2 monster(s) randomly
    int numMonsters = rand() % 2 + 1; // 随机生成 1 或 2
    std::cout << "You will face " << numMonsters << " monster(s)!\n";

    // 清空当前房间敌人列表
    enemies.clear();

    // 从全体怪物中随机选择怪物
    // choose the monster(s) for all enemies
    if (numMonsters == 2){
        const std::vector<Enemy>& allEnemies = getAllCommonEnemies();
        for (int i = 0; i < numMonsters; ++i) {
            int monsterIndex = rand() % allEnemies.size(); // 随机选择怪物
            enemies.push_back(allEnemies[monsterIndex]);
        }
    }else{
        const std::vector<Enemy>& allEnemies = getAllEliteEnemies();
        for (int i = 0; i < numMonsters; ++i) {
            int monsterIndex = rand() % allEnemies.size(); // 随机选择怪物
            enemies.push_back(allEnemies[monsterIndex]);
    }
}
    // 开始与敌人战斗
    // Start battle with the enemy
    if (hasEnemies()) {
        for (auto& enemy : enemies) {
            std::cout << ">> " << enemy.toString() << "\n";

            if (!enemy.isAlive()) continue;

            std::cout << "\nBattle Start!\n";
            player * enemy;

            if (!player.isAlive()) {
                std::cout << "You were defeated by " << enemy.getName() << "!\n";
                return;
            } else if(!enemy.isAlive()){
                std::cout << "You defeated " << enemy.getName() << "!\n";
                unsigned gold = rand() % 21 + 10;
                std::cout << "You gained " << gold << " gold.\n";
                player.addGold(gold);

                // 随机掉落彩蛋字条
                // Random Easter egg notes
                if ((rand() % 100) < 10) {
                    std::cout << "You found a mysterious note...\n";
                    std::cout << "It says: 'Escape code is [letmein]'.\n";
                }
                // 怪物掉落装备
                // Monsters drop equipment
                if ((rand() % 100) < 25) {
                    const std::vector<Item>& allItems = getAllShopItems();
                
                    std::vector<Item> equipmentPool;
                    for (const auto& item : allItems) {
                        if (item.type == Item::Type::Weapon || item.type == Item::Type::Armor) {
                            equipmentPool.push_back(item);
                        }
                    }
                
                    if (!equipmentPool.empty()) {
                        int dropIndex = rand() % equipmentPool.size();
                        const Item& droppedItem = equipmentPool[dropIndex];
                
                        std::cout << "You found a new item: [" << droppedItem.name << "]!\n";
                        player.equipItem(droppedItem);
                    }
                }
            }
        }

        enemies.clear(); // 清空敌人列表
    } else {
        std::cout << "No monsters here.\n";
    }
}

